package com.qiqidream.admin.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatis-plus自动填充配置
 * @author QiQiDream
 * @since 2019/4/16 15:48
 */
@Slf4j
@Component
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("deleted",false,metaObject);
        this.setFieldValByName("updateDate",new Date(),metaObject);
        this.setFieldValByName("createDate",new Date(),metaObject);


        String sort = "sort";
        if (metaObject.hasSetter(sort) && null==metaObject.getValue(sort)){
            this.setFieldValByName("sort",999,metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateDate",new Date(),metaObject);
    }
}
